package com.idanchuang.component.businesslog.es.index;

import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Objects;

/**
 * The TransitionsPolicy
 *
 * @since 2020/7/17
 */
public class TransitionsPolicy implements ToXContentObject {

    static final ParseField STATES_NAME = new ParseField("state_name");
    static final ParseField CONDITIONS = new ParseField("conditions");

    private final String name;
    private final ActionRole condition;

    public TransitionsPolicy(String statesName, ActionRole condition) {
        this.name = statesName;
        this.condition = condition;
    }

    public String getName() {
        return name;
    }

    @Override
    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
        builder.startObject();
        builder.field(STATES_NAME.getPreferredName(), name);
        if (condition != null) {
            builder.field(CONDITIONS.getPreferredName(), condition);
        }
        builder.endObject();
        return builder;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, condition);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }
        TransitionsPolicy other = (TransitionsPolicy) obj;
        return Objects.equals(name, other.name) &&
                Objects.equals(condition, other.condition);
    }

    @Override
    public String toString() {
        return Strings.toString(this);
    }

}
